package com.yuhan.common.base;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.yuhan.common.enums.publicEnum;
import com.yuhan.common.utils.UuidUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang.StringUtils;

import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import java.io.Serializable;
import java.util.Date;

/**
 * @Description:
 * @Author: duff
 * @Date: 2019/6/17 13:49
 */
@MappedSuperclass
@Data
public class BaseEntity<T> implements Serializable {

    @Id
    @ApiModelProperty("主键")
    private String id;

    @ApiModelProperty("创建日期")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createDate;

    @ApiModelProperty("创建人ID")
    private String createUser;

    @ApiModelProperty("是否删除，0为未删除")
    private int delFlag;

    @ApiModelProperty("修改日期")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date lastModifiedDate;

    @ApiModelProperty("修改人ID")
    private String lastModifiedUser;

    @PrePersist
    public void preSave() {
        Date dateTime = new Date();
        if (StringUtils.isBlank(this.getId())) {
            this.setId(UuidUtils.getUUID());
            this.setCreateDate(dateTime);
            this.setLastModifiedDate(dateTime);
            this.setDelFlag(publicEnum.delFlag.DATA_FLAG_OK.value);
        } else {
            this.setLastModifiedDate(dateTime);
        }
    }

}